#include "../tool/headFile.h"
#include "../tool/sock.h"
#include "../tool/struct.h"
#pragma pack(1)
typedef struct Mark{
	int id;
	char mark_name[50];
	int rows;
}Mark;
typedef struct Goods{
	int id;
	char goods_name[50];
	int price;
	char mark_name[50];
	int rows;
}Goods;
typedef struct shopping{
	char goods_name[50];
	int price;
	char mark_name[50];
	int id;
	char phone;
}shopping;
#pragma pack()
//变量
char ip[20]="127.0.0.1";
int port=7001;

char phoneNum[50];
char shuru[50];
char readStr[2048];
char writeStr[100];
Mark* markArr;
Goods* goodsArr;

void showGoods(){
	int a=atoi(shuru);
	sprintf(writeStr,"getGoods,%s",markArr[a-1].mark_name);
	printf("%s\n",writeStr);
	sendCmdToSrv(ip,port,writeStr,100,readStr,2048);
	Goods* strl=(Goods*)readStr;
	int rows=strl[0].rows;
	goodsArr=(Goods*)realloc(goodsArr,rows*sizeof(Goods));
	printf("-----%s-----\n",markArr[a].mark_name);
	printf("餐品号，餐品名，单价\n");
	for(int i=0;i<rows;i++){
		goodsArr[i]=strl[i];
		printf("%d  %s  %d\n",strl[i].id,strl[i].goods_name,strl[i].price);
	}
	printf("Q:返回主菜单\n");
	printf("请输入选择：\n");
	scanf("%s",shuru);
	if(strcmp(shuru,"Q")!=0){
		a=atoi(shuru);
		sprintf(writeStr,"getShop,%s,%d,%s,%s",goodsArr[a-1].goods_name,goodsArr[a-1].price,goodsArr[a-1].mark_name,phoneNum);
		sendCmdToSrv(ip,port,writeStr,100,readStr,2048);
		printf("%s\n",writeStr);
		printf("添加购物车成功！\n");
	}

}

void showMark(){
	sprintf(writeStr,"getAllMark,%s",phoneNum);
	sendCmdToSrv(ip,port,writeStr,100,readStr,2048);
	Mark* strl=(Mark*)readStr;
	int rows=strl[0].rows;
	markArr=(Mark*)realloc(markArr,rows*sizeof(Mark));
	printf("----自主餐饮终端----\n");
	printf("商家号，商家名\n");
	for(int i=0;i<rows;i++){
		markArr[i]=strl[i];
		printf("%d,%s\n",strl[i].id,strl[i].mark_name);
	}
	printf("A:查看购物车\n");
	printf("B:查看订单\n");
	printf("Q:切换用户\n");
}


int main(){
	while(1){
		printf("------自主餐饮终端------\n");
		printf("请输入手机号/或刷卡\n");
		scanf("%s",phoneNum);
		showMark();
		printf("请输入选择：\n");
		scanf("%s",shuru);
		while(1){
			if(strcmp(shuru,"Q")==0){
				break;
			}else{
				showGoods();
			}
		}

		//释放关闭
		free(markArr);
		markArr=NULL;
	}
	return 0;
}
